package com.zhongcw.test.spring.argument.resolver;

import com.zhongcw.test.spring.argument.resolver.entity.CPU;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author ZCW
 * @createTime 2017/11/29
 * @lastUpdateTime 2017/11/29 ZCW
 */
public class MyFirstResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {

        return parameter.getParameterType().equals(CPU.class);

    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        String brand = webRequest.getParameter("brand");
        String series = webRequest.getParameter("series");

        return new CPU(brand,series);
    }
}
